﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Acms.Bll;
using Acms.AcmsControls;

public partial class Templates_SystemHolder_Default : Acms.AcmsControls.TemplatePage
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        dsChildren.SelectData += new EventHandler<Acms.DataControls.SelectEventArgs<Acms.Bll.Post>>(dsChildren_SelectData);

        //dsPageChildren.SelectData += new EventHandler<Acms.DataControls.SelectEventArgs<Acms.Bll.Post>>(dsPageChildren_SelectData);
        dlPageChildren.ItemDataBound += new EventHandler<Acms.WebControls.ItemDataBoundEventArgs>(dlPageChildren_ItemDataBound);
        
        dlFeed.ItemDataBound += new EventHandler<Acms.WebControls.ItemDataBoundEventArgs>(dlFeed_ItemDataBound);
        dsSibling.SelectData += new EventHandler<Acms.DataControls.SelectEventArgs<Acms.Bll.Post>>(dsSibling_SelectData);
        dlNavSiblings.ItemDataBound += new EventHandler<Acms.WebControls.ItemDataBoundEventArgs>(dlNavSiblings_ItemDataBound);
        dsTabs.SelectData += new EventHandler<Acms.DataControls.SelectEventArgs<Post>>(dsTabs_SelectData);

        dsParent.SelectData += new EventHandler<Acms.DataControls.SelectEventArgs<Acms.Bll.Post>>(dsParent_SelectData);
        dlNavParent.ItemDataBound += new EventHandler<Acms.WebControls.ItemDataBoundEventArgs>(dlNavParent_ItemDataBound);
    }
    protected string GetFlash()
    {
        return "http://" + HttpContext.Current.Request.Url.Host + ResolveUrl("~/Flash/Systems.swf");
    }
    

    void dsTabs_SelectData(object sender, Acms.DataControls.SelectEventArgs<Post> e)
    {
        var tabs = new List<Post>();
        foreach(var child in AcmsContext.CurrentPost.Children)
        {
            tabs.AddRange(child.Children.Where(x => x.TemplateName == "TabHolder-NodeParent").ToList());
        }
        e.SetData(tabs);
    }

    //List<Post> itemChildren;
    //Post[] itemChildren;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataBind();
        }

        /*var post = AcmsContext.CurrentPost;

        if (post != null)
        {
            //var itemChildren = post.Children.Where(x => x.TemplateName == "TabHolder").ToArray();
            ///
            
            var findChildren = post.Children;
            foreach (Post p in findChildren)
            {
               
                //var foundItem = p.Children.Where(x => x.TemplateName == "TabHolder").ToArray();
                //foreach (Post i in p.Children.Where(x => x.TemplateName == "TabHolder"))
                //var pp = ;
                foreach (Post i in p.Children.Where(x => x.TemplateName == "TabHolder"))
                {

                    itemChildren.Add(i);
                }

                //p.Children.Where(x => x.TemplateName == "TabHolder").ToArray();
            }
            
            rptTabs.DataSource = itemChildren;
            rptTabs.DataBind();
        }*/
    }
    
    void dlFeed_ItemDataBound(object sender, Acms.WebControls.ItemDataBoundEventArgs e)
    {
        var post = e.DataItem as Post;
        if (post != null)
        {
            var pnlItem = e.Item.FindControl("pnlItem") as Panel;
            ///

            if (pnlItem != null)
            {
                //pnlItem.BackImageUrl = "~/Images/dbImages/systems-1.jpg";
                //var control = acmsPage.AcmsContext.CurrentPost.FindPostControl("Backgound Image");

                var HeroImage  = post.FindPostControl("HeroImage");// as ImageContentControlDisplay;
                if (HeroImage != null)
                {
                    var prop = HeroImage.GetProperties<Acms.AcmsControls.ImageContentControlProperties>();
                    if (prop != null && prop.HasResource())
                    {
                        pnlItem.BackImageUrl = Acms.AcmsControls.RenderFile.GetUrl(prop.ResourceId, this.Page);
                    }
                }
            }

            if (pnlItem != null && e.DataItem == dlFeed.Items[0].DataItem)
            {
                pnlItem.CssClass += "active";
            }
            else
            {
                pnlItem.Style.Add("display", "none");
            }
            for (int i = 1; i <= 5; i++)
            {
                var phPopup = e.Item.FindControl("phPopup" + i) as PlaceHolder;
                var lnk = e.Item.FindControl("lnk" + i) as HyperLink;
                var acmsTitle = e.Item.FindControl("acmsTitle" + i) as TextContentControlDisplay;
                var acmsCopy = e.Item.FindControl("acmsCopy" + i) as HtmlContentControlDisplay;
                SetPopup(post, phPopup, lnk, acmsTitle, acmsCopy, i);
            }
        }
    }


    void dlPageChildren_ItemDataBound(object sender, Acms.WebControls.ItemDataBoundEventArgs e)
    {
        var post = e.DataItem as Post;
        if (post != null)
        {
            var lnk = e.Item.FindControl("lnkNavChildren") as HyperLink;

            if (lnk != null)
            {
                lnk.Text = post.DisplayName;
                lnk.NavigateUrl = post.Url;
            }
        }
    }
    


    private void SetPopup(Post post, PlaceHolder phPopup, HyperLink lnk, TextContentControlDisplay acmsTitle, HtmlContentControlDisplay acmsCopy, int i)
    {
        if (post == null)
            throw new ArgumentNullException("post", string.Format("No post for index {0}", i));
        if (phPopup == null)
            throw new ArgumentNullException("phPopup", string.Format("No phPopup for index {0}", i));
        if (lnk == null)
            throw new ArgumentNullException("lnk", string.Format("No lnk for index {0}", i));
        if (acmsTitle == null)
            throw new ArgumentNullException("acmsTitle", string.Format("No acmsTitle for index {0}", i));
        if (acmsCopy == null)
            throw new ArgumentNullException("acmsCopy", string.Format("No acmsCopy for index {0}", i));

        phPopup.Visible = true;
        if (acmsTitle.IsEmpty)
        {
            phPopup.Visible = false;
        }
        else
        {
            lnk.Text =  acmsTitle.Text;
        }
        if (acmsCopy.IsEmpty)
            phPopup.Visible = false;

        var x = (string)null;
        var posX = post.FindPostControl("Pop" + i + "_X");
        if (posX != null)
        {
            var posXProperty = posX.GetProperties<Acms.AcmsControls.TextContentControlProperties>();
            if (posXProperty != null)
            {
                x = posXProperty.Text.Trim();
            }
        }

        var y = (string)null;
        var posY = post.FindPostControl("Pop" + i + "_Y");
        if (posY != null)
        {
            var posYProperty = posY.GetProperties<Acms.AcmsControls.TextContentControlProperties>();
            if (posYProperty != null)
            {
                y = posYProperty.Text.Trim();
            }
        }

        if (!string.IsNullOrEmpty(x) && !string.IsNullOrEmpty(y))
        {
            lnk.Style.Add("left", x + "px");
            lnk.Style.Add("top", y + "px");
        }
        else
        {
            phPopup.Visible = false;
        }
    }

    

    void dsSibling_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Post> e)
    {
        e.SetData(AcmsContext.CurrentPost.Parent.Children.OrderBy(x => x.Sequence).ToList());
    }

    void dlNavSiblings_ItemDataBound(object sender, Acms.WebControls.ItemDataBoundEventArgs e)
    {
        var post = e.DataItem as Post;
        if (post != null)
        {
            var lnk = e.Item.FindControl("lnkNavSibling") as HyperLink;

            if (lnk != null)
            {
                lnk.Text = post.DisplayName;
                lnk.NavigateUrl = post.Url;
            }
        }
    }

    void dsPageChildren_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Post> e)
    {
        e.SetData(AcmsContext.CurrentPost.Children.OrderBy(x => x.Sequence).ToList());
    }

    void dsChildren_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Post> e)
    {
        e.SetData(AcmsContext.CurrentPost.Children.OrderBy(x => x.Sequence).ToList());
    }

    void dsTabChildren_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Post> e)
    {
        e.SetData(AcmsContext.CurrentPost.Children.Where(x => x.TemplateName == "TabItem").OrderBy(x => x.Sequence).ToList());
    }

    void dsParent_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Post> e)
    {
        e.SetData(AcmsContext.CurrentPost.Parent.Parent.Children.OrderBy(x => x.Sequence).ToList());
    }

    void dlNavParent_ItemDataBound(object sender, Acms.WebControls.ItemDataBoundEventArgs e)
    {
        var post = e.DataItem as Post;
        if (post != null)
        {
            var lnk = e.Item.FindControl("lnkNav") as HyperLink;

            if (lnk != null)
            {
                lnk.Text = post.DisplayName;
                lnk.NavigateUrl = post.Url;
            }
        }
    }
}
